<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Creditor_model extends CI_Model {
	function __construct(){
        parent::__construct();
    }
    
    /**
     * 可转让债权列表
     * @param $user_id
     */
    function get_mb_transfer_list($data){
		//$firstday = date("Y-m-01",strtotime($date));
  		//$lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
  		//3个月内不准转让
  		$user_id = $data['user_id'];
    	$time = date('Y-m-d');
    	$time = strtotime(date("Y-m-d",strtotime("$time -3 month")));
    	
    	$sql = "SELECT p3.id AS borrow_id,p3.title,p1.tender_id,p2.account,p2.interest,p2.repayment_account,p2.repayment_yesaccount,p2.wait_interest,SUM(p1.repay_account) AS t_repay_account,MAX(p1.repay_time) AS last_repay_time  
			FROM rsh_borrow_collection AS p1 LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id 
			LEFT JOIN rsh_borrow AS p3 ON p1.borrow_id=p3.id 
			WHERE p1.user_id={$user_id} AND p1.fstatus=0 AND p3.ftype=4 AND p2.add_time<{$time} AND p2.account>=10000 AND p2.transfer_status=0 
			GROUP BY p1.tender_id ORDER BY last_repay_time ASC";
    	
    	$query = $this->db->query($sql);
    	$list = $query->result_array();
    	return $list;
    }
    
    /**
     * 检查tender_id存不存在rsh_creditor_obj表
     * @param $tender_id
     */
    function check_tender_id($tender_id){
    	$sql = "select id from rsh_creditor_obj where tender_id={$tender_id} and fstatus in(1,2,3)";
    	$query = $this->db->query($sql);
    	return $query->row_array();
    }
    
    /**
     * 申请发布债权转让
     */
    function apply_transfer($data){
    	$this->db->insert('rsh_creditor_obj', $data);
		$ret = $this->db->insert_id();
		if ($ret>0){
			$this->db->where('id',$data['tender_id']);
			$query = $this->db->update('rsh_borrow_tender', array('transfer_status'=>1));
			$ret = $this->db->affected_rows();
		}
		return $ret;
    }
    
    /**
     * 债权转让项目列表
     */
    function transfer_obj_list($data){
    	$_where = " where p1.id>0";
    	if (isset($data['fstatus']) && $data['fstatus']>0){
    		$_where .= " and p1.fstatus='{$data['fstatus']}'";
    	}
    	if (isset($data['title']) && $data['title']!=''){
    		$_where .= " and p1.title='{$data['title']}'";
    	}
    	if (isset($data['user_id']) && $data['user_id']!=''){
    		$_where .= " and p2.user_id='{$data['user_id']}'";
    	}
    	if (isset($data['username']) && $data['username']!=''){
    		$_where .= " and p3.username='{$data['username']}'";
    	}
    	
    	
    	$_order = "";
		if (isset($data['order']) && $data['order']=='add_time'){
    		$_order = " order by p1.add_time desc";
    	}elseif (isset($data['order']) && $data['order']=='fstatus'){
    		$_order = " order by p1.fstatus asc,id desc";
    	}
    	$_limit = "";
    	if (isset($data['per_page']) && isset($data['page_size'])){
    		$_limit = " limit {$data['per_page']},{$data['page_size']}";
    	}
    	$sql = "SELECT count(p1.id) as count FROM rsh_creditor_obj AS p1 
			LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id 
			LEFT JOIN rsh_user AS p3 ON p2.user_id=p3.user_id ".$_where;
    	$query = $this->db->query($sql);
		$count = $query->row()->count;
		
		$sql = "SELECT p1.*,p2.user_id,p2.borrow_id,p2.wait_account,p2.account,p3.username FROM rsh_creditor_obj AS p1 
			LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id 
			LEFT JOIN rsh_user AS p3 ON p2.user_id=p3.user_id ".$_where.$_order.$_limit;
		$query = $this->db->query($sql);
		$list = $query->result_array();
		
		return array(
			'count' => $count,
			'list' => $list
		);
    	
    }
    
    /**
     * 待复审列表
     */
    function reverify_list(){
    	$sql = "SELECT p1.*,p2.user_id,p2.borrow_id,p2.wait_account,p2.account,p3.username FROM rsh_creditor_obj AS p1 
			LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id 
			LEFT JOIN rsh_user AS p3 ON p2.user_id=p3.user_id 
			WHERE p1.fstatus=2 AND p1.discount_yes>=p1.discount_account";
    	$query = $this->db->query($sql);
		return $query->result_array();
    }
    
    /**
     * 获取详情
     * @param $id
     */
    function get_obj_by_id($id){
    	$sql = "SELECT p1.*,p2.user_id,p2.borrow_id,p2.wait_account,p2.account,p2.interest,p3.username FROM rsh_creditor_obj AS p1 
			LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id 
			LEFT JOIN rsh_user AS p3 ON p2.user_id=p3.user_id WHERE p1.id={$id}";
    	$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
    }
    
    function get_by_log_id($id){
    	$sql = "select * from rsh_creditor_log where id = {$id}";
    	$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
    }
    
    /**
     * 根据投资ID获取待收明细
     */
    function get_collection_by_tenderid($tender_id){
    	$sql = "select * from rsh_borrow_collection where tender_id={$tender_id}";
    	$query = $this->db->query($sql);
    	return $query->result_array();
    }
    
    /**
     * 修改
     * @param $setArr
     * @param $id
     */
    function update_by_id($id, $setArr){
    	$this->db->where('id',$id);
		$this->db->update('rsh_creditor_obj', $setArr);
		return $this->db->affected_rows();
    }
    
    /**
     * 首页债权转让列表
     */
    function index_list($data){
		$sql = "SELECT p1.*,p1.discount_yes/p1.discount_account as jindu, p2.user_id,p2.borrow_id,p2.wait_account,p2.account,p3.username,b.apr,b.is_day,b.time_limit_day,b.time_limit FROM rsh_creditor_obj AS p1 
			LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id
			LEFT JOIN rsh_borrow AS b ON  p2.borrow_id = b.id
			LEFT JOIN rsh_user AS p3 ON p2.user_id=p3.user_id WHERE p1.id not in(1,2,3,4,55,56) and p1.fstatus in(2,3) and p2.account>=10000 order by jindu asc,p1.verify_time desc limit 3";
		$query = $this->db->query($sql);
		$list = $query->result_array();
		return $list;
    }
    
    /**
     * 债权转让列表
     */
    function get_list($data){
    	$_limit = "";
    	if (isset($data['per_page']) && isset($data['page_size'])){
    		$_limit = " limit {$data['per_page']},{$data['page_size']}";
    	}
    	
    	$sql = "SELECT count(p1.id) as count FROM rsh_creditor_obj AS p1 
			LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id 
			LEFT JOIN rsh_user AS p3 ON p2.user_id=p3.user_id WHERE p1.id not in(1,2,3,4,55,56) and p1.fstatus in(2,3) and p2.account>=10000";
    	$query = $this->db->query($sql);
		$count = $query->row()->count;
		
		$sql = "SELECT p1.*,p1.discount_yes/p1.discount_account as jindu, p2.user_id,p2.borrow_id,p2.wait_account,p2.account,p3.username FROM rsh_creditor_obj AS p1 
			LEFT JOIN rsh_borrow_tender AS p2 ON p1.tender_id=p2.id 
			LEFT JOIN rsh_user AS p3 ON p2.user_id=p3.user_id WHERE p1.id not in(1,2,3,4,55,56) and p1.fstatus in(2,3) and p2.account>=10000 order by jindu asc,p1.verify_time desc ".$_limit;
		$query = $this->db->query($sql);
		$list = $query->result_array();
		
		return array(
			'count' => $count,
			'list' => $list
		);
    }
    
    /**
     * 债权转让
     */
    function creditor_transfer($data){
		//事务开始
    	$this->db->trans_start();
    	//修改已转让金额
		$sql = "update rsh_creditor_obj set discount_yes=discount_yes+{$data['discount_buy']} where id={$data['obj_id']}";
		$this->db->query($sql);
		//添加转让记录
		$this->db->insert('rsh_creditor_log', $data);
		//修改账户
		$this->load->model('Account_model');
		$user_id = $data['user_id'];
		$mode = substr(strval($user_id),-1,1);
		$account_result 	= $this->Account_model->get_account($user_id);
		$log['user_id'] 	= $user_id;
		$log['ftype'] 		= CREDITOR_FEEEZE;
		$log['money'] 		= $data['discount_buy'];
		$log['total'] 		= $account_result['total'];
		$log['use_money'] 	= $account_result['use_money']-$log['money'];
		$log['no_use_money']= $account_result['no_use_money']+$log['money'];
		$log['collection']  = $account_result['collection'];
		$log['to_user'] 	= 1;
		$log['remark'] 		= "购买债权资金冻结";
		$log['add_time'] 	= $data['add_time'];
		$log['addip'] 		= $this->input->ip_address();
		$this->db->insert('rsh_account_log_'.$mode, $log);
		$this->db->query("update rsh_account set use_money=use_money-{$log['money']},no_use_money=no_use_money+{$log['money']} where user_id={$user_id}");
		//提交事务
		$this->db->trans_complete();
		if($this->db->trans_status() == 1){
			return true;
		}else{
			return false;
		}
    }
    
    /**
     * 根据债权项目ID获取债权购买记录
     * @param $obj_id
     */
    function get_buyers($obj_id){
    	$sql = "SELECT p1.*,p2.username FROM rsh_creditor_log AS p1 LEFT JOIN rsh_user AS p2 ON p1.user_id=p2.user_id WHERE p1.obj_id={$obj_id}";
    	$query = $this->db->query($sql);
    	return $query->result_array();
    }
    
    function get_buyers_uid_by_id($obj_id,$uid,$id){
    	$sql = "SELECT p1.*,p2.username,p2.realname,p2.card_id FROM rsh_creditor_log AS p1 LEFT JOIN rsh_user AS p2 ON p1.user_id=p2.user_id WHERE p1.obj_id={$obj_id} and p1.user_id={$uid} and p1.id={$id}";
    	$query = $this->db->query($sql);
    	return $query->row_array();
    }
    
    /**
     * 根据投资ID获取债权购买记录
     * @param $tender_id
     */
    function get_buyers_by_tenderid($tender_id){
    	//$sql = "SELECT p1.*,p2.username FROM rsh_creditor_log AS p1 LEFT JOIN rsh_user AS p2 ON p1.user_id=p2.user_id WHERE p1.obj_id={$obj_id}";
    	$sql = "select * from rsh_creditor_log where tender_id={$tender_id}";
    	$query = $this->db->query($sql);
    	return $query->result_array();
    }

    /**
     * 根据用户ID获取债权购买记录
     * @param $user_id
     */
    function my_creditor_log($data){
    	
    	$_order = " order by p1.id desc";
    	$_limit = " limit {$data['per_page']},{$data['page_size']}";
    	
    	//$sql = "SELECT count(p1.id) as count FROM rsh_creditor_log AS p1 LEFT JOIN rsh_creditor_obj AS p2 ON p1.obj_id=p2.id  WHERE p1.user_id={$data['user_id']}";
    	$sql = "SELECT count(p1.id) as count FROM rsh_creditor_log AS p1 LEFT JOIN rsh_creditor_obj AS p2 ON p1.obj_id=p2.id LEFT JOIN rsh_borrow_tender AS p3 ON p2.tender_id = p3.id WHERE p1.user_id={$data['user_id']}";
    	$query = $this->db->query($sql);
		$count = $query->row()->count;
		
		
		//$sql = "SELECT p1.*,p2.title,p2.discount_apr FROM rsh_creditor_log AS p1 LEFT JOIN rsh_creditor_obj AS p2 ON p1.obj_id=p2.id WHERE p1.user_id={$data['user_id']}".$_order.$_limit;
		
		$sql = "SELECT p1.*,p2.title,p2.discount_apr,p3.borrow_id FROM rsh_creditor_log AS p1 
				LEFT JOIN rsh_creditor_obj AS p2 ON p1.obj_id=p2.id 
				LEFT JOIN rsh_borrow_tender AS p3 ON p2.tender_id = p3.id
				WHERE p1.user_id={$data['user_id']}".$_order.$_limit;
		
		
		$query = $this->db->query($sql);
		$list = $query->result_array();
		
		return array(
			'count' => $count,
			'list' => $list
		);
    }
    
    /**
     * 初审
     * @param $data
     */
    function verify($data){
    	$id = $data['id'];
    	$fstatus = $data['fstatus'];
    	$admin = isset($data['admin']) ? $data['admin'] : 0;
    	$time = time();
    	$tender_id = $data['tender_id'];
    	
    	//事务开始
    	$this->db->trans_start();
    	
    	$setArr = array(
			'fstatus' => $fstatus,
			'verify_time' => $time,
    		'verify_remark' => isset($data['verify_remark']) ? $data['verify_remark'] : '',
			'verify_user' => $admin
		);
		$ret = self::update_by_id($id,$setArr);
    	if ($ret){
			if ($fstatus==2){
				$sql = "update rsh_borrow_tender set transfer_status=2 where id={$tender_id}";
			}elseif ($fstatus==4){
				$sql = "update rsh_borrow_tender set transfer_status=0 where id={$tender_id}";
			}
			$this->db->query($sql);
		}
		
    	//提交事务
		$this->db->trans_complete();
		if($this->db->trans_status() == 1){
			return true;
		}else{
			return false;
		}
    }
    
    /**
     * 复审通过
     */
    function reverify_true($id){
    	//事务开始
    	$this->db->trans_start();
    	
    	$this->load->model('Account_model');
    	$time = time();
    	$ip = $this->input->ip_address();
    	
    	$obj = self::get_obj_by_id($id);
    	
    	//修改状态
    	$setArr = array(
    		'fstatus' => 3,//已完成
    		'repeat_verify_time' => $time,
    		'repeat_verify_user' => 1,
    	);
    	self::update_by_id($id,$setArr);
    	//修改tender表状态
    	$this->db->where('id',$obj['tender_id']);
    	$setArr = array(
    		'transfer_status' => 3//已转让
    	);
		$this->db->update('rsh_borrow_tender', $setArr);
		
		//购买债权列表
    	$buyList = self::get_buyers($id);
    	foreach ($buyList as $val){
    		//扣除冻结金
			$user_id = $val['user_id'];
			$mode = substr(strval($user_id),-1,1);
			
			$account_result 	= $this->Account_model->get_account($user_id);
			$log['user_id'] 	= $user_id;
			$log['ftype'] 		= CREDITOR_NOFEEEZE;
			$log['money'] 		= $val['discount_buy'];
			$log['total'] 		= $account_result['total']-$log['money'];
			$log['use_money'] 	= $account_result['use_money'];
			$log['no_use_money']= $account_result['no_use_money']-$log['money'];
			$log['collection']  = $account_result['collection'];
			$log['to_user'] 	= 1;
			$log['remark'] 		= "购买债权成功扣除冻结款";
			$log['add_time'] 	= $time;
			$log['addip'] 		= $ip;
			$this->db->insert('rsh_account_log_'.$mode, $log);
			$this->db->query("update rsh_account set total=total-{$log['money']}, no_use_money=no_use_money-{$log['money']} where user_id={$user_id}");
			unset($log);
			//添加待收金额
			//repay_account
			
			$new_repay_account = intval($obj['wait_account'] * ($val['discount_buy']/$val['discount_account']) *100)/100;
			$account_result = $this->Account_model->get_account($user_id);
			$log['user_id'] 	= $user_id;
			$log['ftype'] 		= CREDITOR_COLL_INCR;
			$log['money'] 		= $new_repay_account;
			$log['total'] 		= $account_result['total']+$log['money'];
			$log['use_money'] 	= $account_result['use_money'];
			$log['no_use_money']= $account_result['no_use_money'];
			$log['collection']  = $account_result['collection']+$log['money'];
			$log['to_user'] 	= 1;
			$log['remark'] 		= "购买债权待收增加";
			$log['add_time'] 	= $time;
			$log['addip'] 		= $ip;
			$this->db->insert('rsh_account_log_'.$mode, $log);
			$this->db->query("update rsh_account set total=total+{$log['money']}, collection=collection+{$log['money']} where user_id={$user_id}");
			unset($log);
			//计算待收   修改购买记录状态-已审核
			$this->db->query("update rsh_creditor_log set repay_account={$new_repay_account},fstatus=2,verify_time={$time} where id={$val['id']}");
			
			
			$this->load->model('Borrow_model');
			$tender_info = $this->Borrow_model->get_borrow_tender_obj($val['tender_id']);
			
			//债权协议书——添加到邮件发送队列
			$creditorMailKey = "creditorMailKey";
			$mailList = $this->mycache->get_array($creditorMailKey);
			$mailList = $mailList ? $mailList : array();
			$mailList[] = array(
				'user_id' => $user_id,
				'borrow_id' => $tender_info['borrow_id'],
				'creditor_id' => $val['id'],
			);
			
			$this->mycache->set_array($creditorMailKey,$mailList);
			
			//债权协议书——原借款标——添加到邮件发送队列
			$oldprotocolMailKey = "oldprotocolMailKey";
			$oldmailList = $this->mycache->get_array($oldprotocolMailKey);
			$oldmailList = $oldmailList ? $oldmailList : array();
			$oldmailList[] = array(
				'user_id' => $user_id,
				'borrow_id' => $tender_info['borrow_id'],
				'tender_id' => $val['tender_id']
			);
			$this->mycache->set_array($oldprotocolMailKey,$oldmailList);
			
			
    	}
    	
    	//出售人待收减少,金额进账
    	//待收减少
    	$user_id = $obj['user_id'];
		$mode = substr(strval($user_id),-1,1);
		$account_result 	= $this->Account_model->get_account($user_id);
		$log['user_id'] 	= $user_id;
		$log['ftype'] 		= CREDITOR_COLL_DECR;
		$log['money'] 		= $obj['wait_account'];
		$log['total'] 		= $account_result['total']-$log['money'];
		$log['use_money'] 	= $account_result['use_money'];
		$log['no_use_money']= $account_result['no_use_money'];
		$log['collection']  = $account_result['collection']-$log['money'];
		$log['to_user'] 	= 1;
		$log['remark'] 		= "出售债权待收减少";
		$log['add_time'] 	= $time;
		$log['addip'] 		= $ip;
		$this->db->insert('rsh_account_log_'.$mode, $log);
		$this->db->query("update rsh_account set total=total-{$log['money']}, collection=collection-{$log['money']} where user_id={$user_id}");
		unset($log);
		
		//出售债权入账
		$account_result 	= $this->Account_model->get_account($user_id);
		$log['user_id'] 	= $user_id;
		$log['ftype'] 		= CREDITOR_SUCCESS;
		$log['money'] 		= $obj['discount_account'];
		$log['total'] 		= $account_result['total']+$log['money'];
		$log['use_money'] 	= $account_result['use_money']+$log['money'];
		$log['no_use_money']= $account_result['no_use_money'];
		$log['collection']  = $account_result['collection'];
		$log['to_user'] 	= 1;
		$log['remark'] 		= "出售债权成功入账";
		$log['add_time'] 	= $time;
		$log['addip'] 		= $ip;
		$this->db->insert('rsh_account_log_'.$mode, $log);
		$this->db->query("update rsh_account set total=total+{$log['money']}, use_money=use_money+{$log['money']} where user_id={$user_id}");
		unset($log);
		
		//扣除手续费
		$fee = round($obj['discount_account']*0.008,2);//成交金额*百分之1
		$account_result 	= $this->Account_model->get_account($user_id);
		$log['user_id'] 	= $user_id;
		$log['ftype'] 		= CREDITOR_SUCC_FEE;
		$log['money'] 		= $fee;
		$log['total'] 		= $account_result['total']-$log['money'];
		$log['use_money'] 	= $account_result['use_money']-$log['money'];
		$log['no_use_money']= $account_result['no_use_money'];
		$log['collection']  = $account_result['collection'];
		$log['to_user'] 	= 1;
		$log['remark'] 		= "出售债权成功扣除手续费";
		$log['add_time'] 	= $time;
		$log['addip'] 		= $ip;
		$this->db->insert('rsh_account_log_'.$mode, $log);
		$this->db->query("update rsh_account set total=total-{$log['money']}, use_money=use_money-{$log['money']} where user_id={$user_id}");
		unset($log);
		
    	//提交事务
		$this->db->trans_complete();
		if($this->db->trans_status() == 1){
			return true;
		}else{
			return false;
		}
    }
    
    /**
     * 复审不通过
     */
    function reverify_false($id){
    	//事务开始
    	$this->db->trans_start();
    	
    	$this->load->model('Account_model');
    	$time = time();
    	$ip = $this->input->ip_address();
    	
    	$obj = self::get_obj_by_id($id);
    	//修改状态
    	$setArr = array(
    		'fstatus' => 4,//已取消
    		'repeat_verify_time' => $time,
    		'repeat_verify_user' => 1,
    	);
    	self::update_by_id($id,$setArr);
    	
    	//修改tender表状态
    	$this->db->where('id',$obj['tender_id']);
    	$setArr = array(
    		'transfer_status' => 0//未转让
    	);
		$this->db->update('rsh_borrow_tender', $setArr);
    	
    	//购买债权列表
    	$buyList = self::get_buyers($id);
    	foreach ($buyList as $val){
    		//解除冻结金
			$user_id = $val['user_id'];
			$mode = substr(strval($user_id),-1,1);
			$account_result 	= $this->Account_model->get_account($user_id);
			$log['user_id'] 	= $user_id;
			$log['ftype'] 		= CREDITOR_FAIL;
			$log['money'] 		= $val['discount_buy'];
			$log['total'] 		= $account_result['total'];
			$log['use_money'] 	= $account_result['use_money']+$log['money'];
			$log['no_use_money']= $account_result['no_use_money']-$log['money'];
			$log['collection']  = $account_result['collection'];
			$log['to_user'] 	= 1;
			$log['remark'] 		= "购买债权失败解除冻结资金";
			$log['add_time'] 	= $time;
			$log['addip'] 		= $ip;
			$this->db->insert('rsh_account_log_'.$mode, $log);
			$this->db->query("update rsh_account set use_money=use_money+{$log['money']},no_use_money=no_use_money-{$log['money']} where user_id={$user_id}");
    	}
    	//修改购买记录状态-已取消
    	$this->db->query("update rsh_creditor_log set fstatus=4 where obj_id={$id}");
    	
    	//提交事务
		$this->db->trans_complete();
		if($this->db->trans_status() == 1){
			return true;
		}else{
			return false;
		}
		
    }
    
    /**
     * 我的待收债权明细
     * @param $data
     */
    function ctlog($data = array()){
    	$user_id = isset($data['user_id']) ? intval($data['user_id']) : 0;
    	if (!$user_id){
    		return false;
    	}
    	$per_page = isset($data['per_page']) ? intval($data['per_page']) : 0;
    	$page_size = isset($data['page_size']) ? intval($data['page_size']) : 10;
    	$limit = ($page-1)*$page_size;
    	$sql = "SELECT p1.repay_account*p4.discount_buy/p4.discount_account AS money,p4.discount_buy,p4.discount_account,p1.repay_time,p4.obj_id,p5.title 
				FROM rsh_borrow_collection AS p1 
				LEFT JOIN rsh_borrow AS p2 ON p1.borrow_id=p2.id 
				LEFT JOIN rsh_borrow_tender AS p3 ON p1.tender_id=p3.id 
				LEFT JOIN rsh_creditor_log AS p4 ON p3.id=p4.tender_id 
				LEFT JOIN rsh_creditor_obj as p5 on p4.obj_id=p5.id 
				WHERE p2.fstatus=3 AND p4.user_id={$user_id} AND p4.fstatus=2 AND p1.fstatus=0 order by p1.repay_time asc limit $per_page,$page_size";
    	$query = $this->db->query($sql);
		$list = $query->result_array();
		$sql = "SELECT count(p1.id) as c
				FROM rsh_borrow_collection AS p1 
				LEFT JOIN rsh_borrow AS p2 ON p1.borrow_id=p2.id 
				LEFT JOIN rsh_borrow_tender AS p3 ON p1.tender_id=p3.id 
				LEFT JOIN rsh_creditor_log AS p4 ON p3.id=p4.tender_id 
				LEFT JOIN rsh_creditor_obj as p5 on p4.obj_id=p5.id 
				WHERE p2.fstatus=3 AND p4.user_id={$user_id} AND p4.fstatus=2 AND p1.fstatus=0";
		$query = $this->db->query($sql);
		$row = $query->row_array();
    	$ret = array(
    		'list'=>$list,
    		'count'=>$row['c']
    	);
    	return $ret;
		
    }
    
    //获取代收最后一条估计还款时间
    function get_creditor_collection_repay_time($where = array()){
    	
    	$this->db->select('repay_time');
    	if($where){
			$this->db->where($where);
		}
    	
    	$query = $this->db->get('rsh_borrow_collection');
		$row = $query->row_array();
			
    	return $row['repay_time'];
    }
    
    
    
}